<?php

require_once('../util/DataBaseUtil.php');
require_once('../util/LogUtil.php');

require_once('../vo/User.php');
require_once('../vo/request/LoginUserRequest.php');
require_once('../vo/response/LoginUserResponse.php');
require_once('../vo/request/SaveUserRequest.php');
require_once('../vo/response/SaveUserResponse.php');

/***********/
/** LOGIN **/
/***********/

function login($request)
{
  $response = new LoginUserResponse();
  connect();

  $sql = "SELECT * FROM `users` WHERE `userName` = '".$request['userName']."';";
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "Identifiant incorrect.";
    return $response;
  }

  $res_user = mysql_fetch_array($res, MYSQL_ASSOC);
  if (!isset($res_user) or ($res_user == 0))
  {
    $response->errorMessage = "Identifiant incorrect.";
    return $response;
  }

  if ($res_user['password'] != $request['password'])
  {
    $response->errorMessage = "Mot de passe incorrect.";
    return $response;
  }

  $user = new User();
  $user->id = $res_user['id'];
  $user->firstName = $res_user['firstName'];
  $user->lastName = $res_user['lastName'];
  $user->userName = $res_user['userName'];
  $user->password = $res_user['password'];
  $response->user = $user;

  $userContacts = array();
  $sql = "SELECT * FROM `contacts` WHERE `userId` = ".$user->id;
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving contacts.";
    log_info($response->errorMessage);
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $c = new Contact();
    $c->init($r['id'], $r['userId'], $r['firstName'], $r['lastName'], $r['address'],  $r['city'], $r['email'], $r['phone'], $r['groupId']);
    $userContacts[] = $c;
  }
  $response->userContacts = $userContacts;

  $userGroups = array();
  $sql = "SELECT * FROM `groups` WHERE `userId` = ".$user->id;
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving groups.";
    log_info($response->errorMessage);
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $g = new Group();
    $g->init($r['id'], $r['userId'], $r['name']);
    log_info("user.group.id = ".$g->name);
    $userGroups[] = $g;
  }
  $response->userGroups = $userGroups;
  
  mysql_close();

  $response->errorMessage = "";
  return $response;
}

/**********/
/** SAVE **/
/**********/

function saveUser($request)
{
  $response = new SaveUserResponse();
  connect();

  $dataUser = $request['user'];

  $user = new User();
  $user->id = $dataUser['id'];
  $user->firstName = $dataUser['firstName'];
  $user->lastName = $dataUser['lastName'];
  $user->userName = $dataUser['userName'];
  $user->password = $dataUser['password'];

  connect();

  log_info("user.id = ".$user->id
          ."\nuserId = ".$request['userId']);

  if ($user->id == 0)
  {
    $query = "INSERT INTO `users` (userId, firstName, lastName, userName, password) "
            ."VALUES ('".$request['userId']."', '".$user->firstName."', '".$user->lastName."', '".$user->userName."', '".$user->password."');";
  }
  else
  {
    $query = " UPDATE `users` SET "
           ." `id`='".$user->id."', "
           ." `firstName`='".$user->firstName."', "
           ." `lastName`='".$user->lastName."', "
           ." `userName`='".$user->userName."', "
           ." `password`='".$user->password."' "
           ." WHERE `id`='".$user->id."';";
  }

  log_info($query);
  mysql_query($query)
    or die ('Une erreur est survenue lors de l\'ajout.<br>'.mysql_error());

  mysql_close();

  return $response;
}

?>
